package cn.com.czpb.client.module.personal.fragment;

import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import cn.com.czpb.client.R;
import cn.com.czpb.client.api.InterfaceJsonfile;
import cn.com.czpb.client.app.BaseFragment;
import cn.com.czpb.client.custorm.CircleImageView;
import cn.com.czpb.client.model.usercenter.IsInvitedBean;
import cn.com.czpb.client.model.usercenter.IsInvitedEntity;
import cn.com.czpb.client.net.Factory;
import cn.com.czpb.client.utils.DeviceUtils;
import cn.com.czpb.client.utils.LogUtils;
import cn.com.czpb.client.utils.ParamUtils;
import cn.com.czpb.client.utils.TUtils;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2017/12/1.
 */

public class InputCodeFragment extends BaseFragment {
    @BindView(R.id.inputcodeet)
    EditText inputcode;
    @BindView(R.id.inputsubmit)
    Button submit;
    @BindView(R.id.invitedrl)
    RelativeLayout invitedrl;
    @BindView(R.id.invited_rl)
    RelativeLayout invited_user;
    @BindView(R.id.invited_img)
    CircleImageView invited_img;
    @BindView(R.id.invited_name)
    TextView invited_name;
    @BindView(R.id.invited_time)
    TextView invited_time;
    public static InputCodeFragment newInstance(){
        InputCodeFragment fragment = new InputCodeFragment();
        return fragment;
    }
    @Override
    public int setMyContentView() {
        return R.layout.fragment_inputcode;
    }

    @Override
    public void initView() {
        getInviteState();
    }

    private void getInviteState() {
        Map<String, String> map = new HashMap<>();
        if(spu.getUser() != null) {
            map.put("uid", spu.getUser().getUid());
        }
        map.put("device", DeviceUtils.getMyUUID(activity));
        Factory.provideHttpService().isinvited(ParamUtils.getRequestParam(map))
                .onBackpressureBuffer()
                .subscribeOn(Schedulers.io())
                .filter(new Func1<IsInvitedEntity, Boolean>() {
                    @Override
                    public Boolean call(IsInvitedEntity myInviteEntity) {
                        return true;
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<IsInvitedEntity>() {
                    @Override
                    public void call(IsInvitedEntity myInviteEntity) {
                        LogUtils.e("code--" + myInviteEntity.code);
                        if ("200".equals(myInviteEntity.code)) {
                            IsInvitedBean isInvitedBean = myInviteEntity.data.get(0);
                            if(isInvitedBean.getFlag().equals("1")) {
                                showInvitedStatus(isInvitedBean);
                            }else{
                              showNotInvitedStatus();
                            }
                        }else{
                            if("209".equals(myInviteEntity.code)){
                                showNotInvitedStatus();
                            }
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        showNotInvitedStatus();
                        LogUtils.i("throwable--" + throwable.getMessage());
                    }
                });
    }

    private void showNotInvitedStatus() {
        inputcode.setVisibility(View.VISIBLE);
        submit.setVisibility(View.VISIBLE);
        invitedrl.setVisibility(View.GONE);
        invited_user.setVisibility(View.GONE);
    }

    private void showInvitedStatus(IsInvitedBean isInvitedBean) {
        inputcode.setVisibility(View.GONE);
        submit.setVisibility(View.GONE);
        invitedrl.setVisibility(View.VISIBLE);
        invited_user.setVisibility(View.VISIBLE);
        Glide.with(activity).load(isInvitedBean.getAvatar_path()).into(invited_img);
        invited_name.setText(isInvitedBean.getUsername());
        invited_time.setText(isInvitedBean.getCreate_time());
    }
    @OnClick(R.id.inputsubmit)
    public void dosubmit(){
        String code = inputcode.getText().toString().trim();
        if(code.equals("")){
            TUtils.toast("请输入邀请码再试");
        }else{
            getInvite(code);
        }

    }

    private void getInvite(String code) {
        Map<String, String> map = new HashMap<>();
        if(spu.getUser() != null) {
            map.put("uid", spu.getUser().getUid());
        }
        map.put("device", DeviceUtils.getMyUUID(activity));
        map.put("invitcode",code);
        map.put("siteid", InterfaceJsonfile.SITEID);
        Factory.provideHttpService().submitcode(ParamUtils.getRequestParam(map))
                .onBackpressureBuffer()
                .subscribeOn(Schedulers.io())
                .filter(new Func1<IsInvitedEntity, Boolean>() {
                    @Override
                    public Boolean call(IsInvitedEntity myInviteEntity) {
                        return true;
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<IsInvitedEntity>() {
                    @Override
                    public void call(IsInvitedEntity myInviteEntity) {
                        LogUtils.e("code--" + myInviteEntity.code);
                        if ("200".equals(myInviteEntity.code)) {
                            IsInvitedBean isInvitedBean = myInviteEntity.data.get(0);
                            showInvitedStatus(isInvitedBean);
                        }else{
                            if("209".equals(myInviteEntity.code)){
                                TUtils.toast("邀请码失效");
                                showNotInvitedStatus();
                            }
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        showNotInvitedStatus();
                        LogUtils.i("throwable--" + throwable.getMessage());
                    }
                });
    }

    @Override
    public void initData() {

    }
}
